package cn.jtool.dig.server.controller;

import cn.jtool.dig.server.domain.reqeust.PortMappingParams;
import cn.jtool.dig.server.domain.reqeust.PortMappingQueryRequest;
import cn.jtool.dig.server.service.PortMappingService;
import cn.jtool.knife.core.result.ResultEntity;
import cn.jtool.knife.core.validator.Create;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/port-mapping")
public class PortMappingController {


    @Resource
    private PortMappingService portMappingService;

    @PostMapping(value = "/create", name = "创建端口映射", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> create(@Validated(value = Create.class) @RequestBody PortMappingParams entity){
        this.portMappingService.create(entity);
        return ResponseEntity.ok(
                ResultEntity.success()
        );
    }

    @PutMapping(value = "/update", name = "更新端口映射", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> update(@Validated(value = Create.class) @RequestBody PortMappingParams entity){
        this.portMappingService.update(entity.getId(), entity);
        return ResponseEntity.ok(
                ResultEntity.success()
        );
    }

    @DeleteMapping(value = "/delete", name = "删除端口映射", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> delete(@RequestParam(value = "id") int id){
        this.portMappingService.delete(id);
        return ResponseEntity.ok(
                ResultEntity.success()
        );
    }

    @GetMapping(value = "/detail", name = "获取端口映射详情", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> detail(@RequestParam(value = "id", defaultValue = "1") int id){
        return ResponseEntity.ok(
                ResultEntity.success(
                        this.portMappingService.selectOne(id)
                )
        );
    }

    @GetMapping(value = "/list", name = "获取端口映射列表", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> list(@RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value = "limit", defaultValue = "10") int limit){
        return ResponseEntity.ok(
                ResultEntity.success(
                        this.portMappingService.listByPage(new PortMappingQueryRequest(), page, limit)
                )
        );
    }

    @GetMapping(value = "/connect/list", name = "获取端口映射的连接列表", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> connectList(@RequestParam(value = "id") int id){

        return ResponseEntity.ok(
                ResultEntity.success(
                        this.portMappingService.connectList(id)
                )
        );
    }

    @GetMapping(value = "/connect/kill", name = "关闭连接", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> kill(@RequestParam(value = "id") int id){
        this.portMappingService.kill(id);
        return ResponseEntity.ok(
                ResultEntity.success()
        );
    }

    @GetMapping(value = "/connect/ban", name = "禁用ip", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> ban(@RequestParam("tunnelId") int tunnelId,
                                               @RequestParam(value = "id") int id){
        this.portMappingService.ban(tunnelId, id);
        return ResponseEntity.ok(
                ResultEntity.success()
        );
    }
    
}
